home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 932 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.3 KB

  1. From: thoff@symantec.com (Torsten Hoff)
  2. Message-ID: <4jq60l$6pp@symiserver2.symantec.com>
  3. X-Original-Date: Tue, 02 Apr 96 00:07:25 GMT
  4. Path: in2.uu.net!bounce-back
  5. Date: 02 Apr 96 13:30:31 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Newsgroups: comp.std.c++
  8. Subject: Re: Const class member
  9. Organization: Symantec Corp.
  10. References: <4jgpqa$t09@nntp.interaccess.com>
  11. X-Newsreader: News Xpress Version 1.0 Beta #4
  12. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  13.     iQBFAgUBMWEsI+EDnX0m9pzZAQHDQAF/ZG6X3PJ16+fPWTc2h6Ei69iIAgSyNTZ0
  14.     2l92eD6qpxVO/OhI4nKyRB0nNA4+G5Sb
  15.     =YCiC
  16.  
  17. In article <4jgpqa$t09@nntp.interaccess.com>,
  18.    brianmcg@interaccess.com (Brian V. McGroarty) wrote:
  19. >Is this legal?  Borland and Microsoft compilers will accept the following:
  20. >
  21. >class AnyClass
  22. >{
  23. >    const int constInt;
  24. >}
  25. >
  26. >The Borland compiler complains about the uninitialized constant, whereas
  27. >the Microsoft compiler does not.  If a constructor is present, both will
  28. >complain that the constant isn't initialized in the constructor, however
  29. >neither will allow you to assign a value in the constructor by simply
  30. >specifying "constInt= some value".  I have also attempted to initialize in
  31. >a global variable "int AnyClass::constInt",  to determine whether static
  32. >somehow became implicit -- still no go.  If this is legal, how is the value
  33. >initialized?
  34. [Snip]
  35.  
  36. That's perfectly legal.
  37.  
  38. However, you can't initialize the const member in the class
  39. declaration, since you haven't instantiated an object of the class in
  40. question which could receive the value. Furthermore, it would prevent
  41. you from assigning different values to constInt in different instances
  42. of the class, which in most cases is not what you want, either.
  43.  
  44. If you *really* want compile-time initialized constants, and can live
  45. with something that has essentially the same properties as an integer,
  46. use class-scope enums. If you need several compile-time constants with
  47. the same value, you can use multiple untagged enums:
  48.  
  49. class AnyClass
  50. {
  51.     enum {FOO = 1, BAR = 1}; // multiple enums with same value; doesn't work!
  52.     enum {FOO = 1};          // OK
  53.     enum {BAR = 1};          // OK
  54. }
  55.  
  56. To continue with the const class member for a moment, you can't
  57. initialize the const class member in your constructor, as you already
  58. noted -- this must be done using the initialization list. You can do
  59. this as follows:
  60.  
  61. AnyClass::AnyClass() : constInt(0)
  62.     {
  63.     // other initialization code goes here
  64.     }
  65.  
  66. or
  67.  
  68. AnyClass::AnyClass(int ConstValue) : constInt(ConstValue)
  69.     {
  70.     // other initialization code goes here
  71.     }
  72.  
  73. The initialization is actually performed before your constructor is
  74. entered; if more than one member is being initialized, it happens in
  75. the order in which the members are declared in the class, *not* the
  76. order in which they are listed in the initialization list.
  77.  
  78. I hope this helps!
  79.  
  80.  
  81. Torsten Hoff
  82. thoff@symantec.com
  83.  
  84. (The views and opinions expressed are my own, and
  85. should not be construed as representing those of
  86. Symantec Corporation)
  87. ---
  88. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  89. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  90. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  91. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  92. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  93.